package com.gurid.service.oss.util;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import java.io.InputStream;
import java.util.UUID;

public class QiniuOssUtil {

    private static String domain = "http://sc6vzb7ct.hd-bkt.clouddn.com";
    private static String bucket = "qspacey1";
    private static String accesskey = "RmbEjJinufdc0Hlmq35JTLKZSz3ewK5K6O9itjWs";
    private static String secretkey = "v2C5zLWzzUDBw9oKCzgMVEy-cNhVuBfsfwJvSed6";

    //构造一个带指定 Region 对象的配置类
    private static Configuration cfg = new Configuration(Region.region0());
    public QiniuOssUtil() {
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本
    }
    private static UploadManager uploadManager = new UploadManager(cfg);
    /**
     * 1.上传本地文件，直接指定文件的完整路径即可上传
     */
    public static String upload(String localFilePath){

        String url = "";
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        //String localFilePath = "/home/qiniu/test.png";
        //String localFilePath = "F:\\Projects\\Java\\forest.jpg";
        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = null;

        //...生成上传凭证，然后准备上传
        Auth auth = Auth.create(accesskey, secretkey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(localFilePath, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);
                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        }
        /**
         * 文件外链地址构成：
         * http://<domain>/<key>?e=<deadline>&token=<downloadToken>
         * domain:sc6vzb7ct.hd-bkt.clouddn.com
         * key:程序中自定义，默认为文件的hash
         * deadline:默认3600秒
         * downloadToken:可以使用SDK提供的方法生成
         */

        url = "http://sc6vzb7ct.hd-bkt.clouddn.com/" + key +
                "?e=1713602886" +
                "&token=" +
                accesskey +
                ":XOwtHG_WC7AeIF5AC1u82zj1W08=";
        return key;
    }

    /**
     * 2.InputStream对象的上传，适用于所有的InputStream子类
     */
    public static String upload(InputStream inputStream){
        String key = UUID.randomUUID() + ".png";
        /*byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
        ByteArrayInputStream byteInputStream=new ByteArrayInputStream(uploadBytes);*/
        Auth auth = Auth.create(accesskey, secretkey);
        String upToken = auth.uploadToken(bucket);
        try {
            Response response = uploadManager.put(inputStream,key,upToken,null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);
                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        }
        return domain + "/" + key + "-gurid";
    }
}
